package controller;

import Bean.CreateFileBean;
import create.CreateController;
import create.CreateVm;
import model.TAlarmActionAcheduleSettingEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import util.AllUtil;
import util.Const;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by eleven on 15/11/7.
 */
@Controller
public class TestController {

    @RequestMapping(value = "/test.htm")
    public String dpr()
    {
        //testquartz t =new testquartz();
        //t.dosometing();
//        Field[] ff = TAlarmActionAcheduleSettingEntity.class.getDeclaredFields();
//        System.out.println(ff[0].getAnnotations());
//        System.out.println(ff[0]);
//        System.out.println(ff[0].getName());
        return "test";
    }

    @RequestMapping(value = "/echart3test.htm")
    public String echartsdpr()
    {
        //testquartz t =new testquartz();
        //t.dosometing();
//        Field[] ff = TAlarmActionAcheduleSettingEntity.class.getDeclaredFields();
//        System.out.println(ff[0].getAnnotations());
//        System.out.println(ff[0]);
//        System.out.println(ff[0].getName());
        return "testmain";
    }

    @RequestMapping(value = "/testajx.htm")
    public @ResponseBody String ddd(HttpServletRequest request, HttpServletResponse response)
    {
//        i="7";
//        List<String> s = new ArrayList();
//        s.add("3");
//        s.add("4");
//        System.arraycopy();

        String i = (String)request.getAttribute("value");
        i=request.getParameter("value");
//        i = request.getContextPath();
        System.out.println(i);
        Map<String,Object> m =new HashMap<String, Object>();
        m.put("dat",i);
        Object o = (Object)i;
//        response.d
        ModelAndView mv = new ModelAndView();
        mv.addObject(m);
        mv.setViewName("testmain");
        return "<option value=555>555</option><option>666</option><option>777</option>";
    }

    @RequestMapping(value = "/createvm.htm")
    public String createvm(@RequestParam(value = "choice", required = false) String choice,
                         @RequestParam(value = "filename", required = false) String filename,
                         @RequestParam(value = "name", required = false) String name,
                         @RequestParam(value = "ruletype", required = false) String ruletype,
                         @RequestParam(value = "ruletypepath", required = false) String ruletypepath,
                         @RequestParam(value = "body", required = false) String body,
                         @RequestParam(value = "path", required = false) String path)
    {
        CreateVm createVm = new CreateVm();
        if (createVm.Create(filename))
            System.out.println("Create Success!!");
        else
            System.out.println("Create Fail!!");
        CreateFileBean createFileBean = new CreateFileBean();
        createFileBean.setChoice(choice);
        createFileBean.setChinesename(name);
        createFileBean.setDispatchname(ruletype);
        createFileBean.setVmpath(ruletypepath);
        createFileBean.setDaogetname(body);
        createFileBean.setDaopath(path);
        if (createVm.Write(createFileBean))
            System.out.println("Vm Write Success!!");
        else
            System.out.println("Vm Write Fail!!");
        return "createcontroller";
    }

    @RequestMapping(value = "/testread.htm")
    public String testread(@RequestParam(value = "path", required = false) String path)
    {
        AllUtil allUtil = new AllUtil();
        for(String s:allUtil.readDaoFile(path)){
            System.out.println(s);
        }
        return "test";
    }

    @RequestMapping(value = "/createcontroller.do")
    public String createcontroller(@RequestParam(value = "chinesename", required = false) String chinesename,
            @RequestParam(value = "way", required = false) String way,
            @RequestParam(value = "name", required = false) String name,
                                   @RequestParam(value = "daoname", required = false) String daoname,
                                   @RequestParam(value = "servicename", required = false) String servicename,
                                   @RequestParam(value = "url", required = false) String url) {
        CreateController createController = new CreateController();
        CreateFileBean createFileBean = new CreateFileBean();
        createFileBean.setChinesename(chinesename);
        way = Const.CONTROLLER_BASE_PATH+way;
        createFileBean.setControllerpath(way);
        createFileBean.setDispatchname(name);
        createFileBean.setDaoname(daoname);
        createFileBean.setServicename(servicename);
        createFileBean.setConsturl(url);
        if (createController.Write(createFileBean)){
            System.out.println("Controller Write Success!!");
        }else{
            System.out.println("Controller Write Fail!!");
        }
        return "createcontroller";
    }

    @RequestMapping(value = "/createcontroller.htm")
    public String testdispatch()
    {
        System.out.println("do");
        return "createcontroller";
    }

    @RequestMapping(value = "/advance.htm")
    public ModelAndView advance()
    {
        AllUtil allUtil =new AllUtil();
        List<String> folder = allUtil.traverseFolder(Const.VM_BASE_PATH);
        List<String> firstFolder = new ArrayList<>();
        List<String> secondFolder = new ArrayList<>();
        for (String s:folder){
            String[] strings = s.split("/");
            firstFolder.add(strings[10]);
            for(String ss:allUtil.traverseFolder(s)){
                String[] strings2 = s.split("/");
                secondFolder.add(strings2[11]);
            }
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("firstfolder",firstFolder);
        mv.addObject("secondfolder",secondFolder);
        mv.setViewName("/advance");
        return mv;
    }
}
